//
//  SetPWViewController.m
//  Login
//
//  Created by mac on 17/3/20.
//  Copyright © 2017年 ZF. All rights reserved.
//

#import "SetPWViewController.h"
#import "NetworkLink.h"
#import "LoginVC.h"

@interface SetPWViewController () <UITextFieldDelegate>
{
    AppDelegate *delegate;
}
@property (weak, nonatomic) IBOutlet UITextField *oldPWTextField;
@property (weak, nonatomic) IBOutlet UITextField *nEwPWTextfield;

@end

@implementation SetPWViewController
- (IBAction)onClick:(id)sender {
    [self finish];
}

- (void)finish
{
    if([self.oldPWTextField.text isEqualToString:@""]||[self.nEwPWTextfield.text isEqualToString:@""])
    {
        [Helps showTips:@"请输入密码" forTime:1 yOffset:1];
        return ;
    }
    else if(![self.oldPWTextField.text isEqualToString:delegate.password])
    {
        [Helps showTips:@"请核对当前密码" forTime:1 yOffset:1];
        return ;
    }
    else if([self.nEwPWTextfield.text isEqualToString:self.oldPWTextField.text])
    {
        [Helps showTips:@"新密码与当前密码一致" forTime:1 yOffset:1];
        return ;
    }
        [NetworkLink setUserName:delegate.userName Password:self.nEwPWTextfield.text Department:NULL Id:delegate.userID complete:^(BOOL ok) {
            if (ok)
            {
                delegate.password = self.nEwPWTextfield.text;
                [delegate.userdefault setObject:delegate.password forKey:@"Password"];
                [delegate.userdefault synchronize];
                [Helps showTips:@"修改成功,请重新登录" forTime:1 inView:[Helps window] yOffset:1];
                LoginVC *vc = [self.storyboard instantiateViewControllerWithIdentifier:@"login"];
                vc.navigationItem.hidesBackButton = YES;
                [self.navigationController pushViewController:vc animated:YES];
            }
        }];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self.oldPWTextField resignFirstResponder];
    [self.nEwPWTextfield resignFirstResponder];
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    if(textField.tag == 8003)
    {
        [self.nEwPWTextfield becomeFirstResponder];
    }
    else if(textField.tag == 8004)
    {
        [self finish];
    }
    return YES;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    delegate = [Helps app];
    self.oldPWTextField.delegate = self;
    self.oldPWTextField.clearButtonMode = UITextFieldViewModeAlways;
    self.oldPWTextField.tag = 8003;
    self.nEwPWTextfield.delegate = self;
    self.nEwPWTextfield.clearButtonMode = UITextFieldViewModeAlways;
    self.nEwPWTextfield.tag = 8004;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
